import { get, post, put, _delete } from "./request"
import API from "./api"
import qs from "querystring"

// 默认 application/x-www-form-urlencoded
// application/json
let config = {
    "Content-Type": "application/json; charset=utf-8"
}

// 登录
export const login = data =>  post(API.LOGIN, data, config)

// 验证 token
export const verifyToken = _ => post(API.VERIFY_TOKEN)

// 退出
export const logout = _ => get(API.LOGOUT)

// 获取首页轮播列表
// export const getCarouselImageList = _ => get(API.CAROUSEL_HOME)

// 菜单列表
export const getMenuList = _ => get(API.MENU_LIST)

// 用户信息数据列表
export const getUserList = data => get(API.USER_LIST, data)

export const searchUser = data => get(`${API.SEARCH_USER}/${data}`)

// 修改用户状态
// export const modifyUserState = data => put(API.MODIFY_USER_STATE, qs.stringify(data))
// restful
export const modifyUserState = ({uid, userState}) => put(`${API.MODIFY_USER_STATE}/${uid}/${userState}`)

// 新增用户
export const addUser = data => post(API.ADD_USER, data, config)

// 编辑用户
// export const editUser = data => get(API.EDIT_USER, data)
// restful
export const editUser = data => get(`${API.EDIT_USER}/${data.uid}`)

// 保存用户
export const saveUser = data => put(API.SAVE_USER, qs.stringify(data))

// 删除用户 (restful)
export const deleteUser = ({uid}) => _delete(`${API.DELETE_USER}/${uid}`)

// 角色列表
export const roleList = _ => get(API.ROLE_LIST)

// 权限列表 (list)
export const authList = _ => get(API.AUTH_LIST)

// 选择角色删除权限 (restful)
export const chooseRoleAndDeleteAuth = ({rid, aid}) => _delete(`${API.CHOOSE_ROLE_AND_DELETE_AUTH}/${rid}/delete-auth/${aid}`)

// 添加角色
export const addRole = data => post(`${API.ADD_ROLE}`, data, config)

// 权限列表 (tree)
export const authTree = _ => get(API.ASSIGN_AUTH)

// 保存分配权限设置
export const saveAssignAuth = ({rid, authTree}) => post(`${API.SAVE_ASSIGN_AUTH}/${rid}`, qs.stringify({authTree}))

// 更新角色权限
export const saveAssignRole = ({uid, roleName}) => put(`${API.SAVE_ASSIGN_ROLE}/${uid}/${roleName}`)

// 商品分类列表 (restful)
export const goodsCateList = ({pages, counts}) => get(`${API.GOODS_CATE_LIST}/${pages}/${counts}`)

// 添加商品分类
export const addGoodsCate = data => post(API.ADD_GOODS_CATE, data, config)

// 删除商品分类
export const deleteGoodsCate = ({cid}) => _delete(`${API.DELETE_GOODS_CATE}/${cid}`)

// 获取参数或属性
export const getGoodsCateAttr = ({cid, sel}) => get(`${API.GOODS_CATE_ATTR}/${cid}/attributes`, {sel})

// 保存分类参数或属性
export const AddCateParamsOrAttr = data => post(API.ADD_CATE_PARAMS_OR_ATTR, qs.stringify(data))

// 修改分类参数或属性名
export const editCateParamsOrAttr = data => put(API.EDIT_CATE_PARAMS_OR_ATTR, qs.stringify(data))

// 删除分类参数或属性
export const deleteCateParamsOrAttr = ({cid, aid, sel}) => _delete(`${API.DELETE_CATE_PARAMS_OR_ATTR}/${cid}/${aid}/${sel}`)

// 添加分类参数或属性标签
export const addCateTags = ({aid, tagName}) => post(`${API.ADD_CATE_TAG}/${aid}`, qs.stringify({tagName}))

// 删除分类参数或属性标签
export const deleteCateTags = ({aid, tagName}) => _delete(`${API.DELETE_CATE_TAG}/${aid}/${encodeURIComponent(tagName)}`)

// 商品列表
export const getGoodsList = ({page, count}) => get(`${API.GOODS_LIST}/${page}/${count}`)

// 添加商品
export const addGoods = data => post(API.ADD_GOODS, data, config)

// 查找商品
export const searchGoods = ({goodsInfo}) => get(`${API.SEARCH_GOODS}/${goodsInfo}`)

// 更新商品
export const editGoods = ({goodsId, goodsInfo}) => put(`${API.EDIT_GOODS}/${goodsId}`, {...goodsInfo}, config)

// 删除商品
export const deleteGoods = ({goodsId}) => _delete(`${API.DELETE_GOODS}/${goodsId}`)

// 上传图片(单文件上传)
export const uploadSingleImage = data => post(API.UPLOAD_SINGLE, data)

// 上传图片(多文件上传)
export const uploadArrayImage = data => post(API.UPLOAD_ARRAY, data)

// 订单列表
export const getOrderList = ({pageNum, pageCount}) => get(`${API.ORDER_LIST}/${pageNum}/${pageCount}`)

// 搜索订单
export const searchOrders = ({msg}) => get(`${API.SEARCH_ORDERS}/${msg}`)

// 获取城市列表
export const getProvinces = _ => get(API.PROVINCES)

// 修改收货地址
export const updateAddr = ({addr, oid}) => put(`${API.MODIFY_ADDR}/${addr}`, {oid}, config)

// 获取物流进度信息
export const getLogisticsProgress = ({lid}) => get(`${API.LOGISTICS_PROGRESS}/${lid}`)

// 获取 echart 数据
export const getEchartsOption = _ => get(API.ECHARTS_OPTION)
